package com.ojoin.ojoin.eb_spider.service.catchdata.suning;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.openqa.selenium.WebElement;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.ojoin.ojoin.eb_spider.service.ISLDataCatchAtomStepService;
import com.ojoin.ojoin.eb_spider.service.WebDriverService;
import com.ojoin.trade.common.utils.WaitUtil;


@Scope("prototype")
@Service
public class SUNINGLogin implements ISLDataCatchAtomStepService<String> {

	public WebDriverService webDriver;
	public String username;
	public String password;
	public String uid;
	public String plateformNane;
	

	@Override
	public void get() throws IOException, Exception {
		//webDriver.initCookies(uid,plateformNane);
		webDriver.get("https://mpassport.suning.com/ids/login?service=https%3A%2F%2Fscs.suning.com%2Fsps%2Fauth%3FtargetUrl%3Dhttp%253A%252F%252Fscs.suning.com%252Fsps%252Fmember%252Flogon.do&loginTheme=scs");
		//webDriver.initCookies(uid,plateformNane);
	}

	@Override
	public void sendKeys() throws Exception {
		webDriver.sendKeys(WebDriverService.ID, "username", username);
		webDriver.sendKeys(WebDriverService.ID, "pwd", password);

		boolean flag = webDriver.IsDisplayed("id", "checkCodeBlock");
		if (flag) {
			WebElement verifyE = webDriver.findElement(WebDriverService.ID,
					"vcodeimg1");
			String code = webDriver.getVerifyCodeByDMV3(verifyE);

			webDriver.sendKeys(WebDriverService.ID, "validateCode", code);
		}
	}

	@Override
	public void submit() {
		webDriver.click("css", "#frmLogin > div > div > div.login-btn > a");

	}

	@Override
	public List<String> result() {
		List<String> li = new ArrayList<String>();
		String text = webDriver
				.getText(
						WebDriverService.CSS,
						"#frmLogin > div > div > div.entry-tip.hide > table > tbody > tr > td:nth-child(2) > span");
		li.add(text);
		return li;
	}

	public boolean isUserAndPassword() {
		String loginReturnMess = result().get(0); 
		if (null != loginReturnMess
				&& ("您输入的账户名与密码不匹配，请重新输入！".trim()
						.equals(loginReturnMess.trim()))) {
			return true;
		}
		return false;
	}

	public boolean isVerifycodeError() {
		String loginReturnMess =result().get(0);
		if (null != loginReturnMess
				&& ("输入的验证码错误，请重新输入！".trim().equals(loginReturnMess.trim()) || "请输入验证码"
						.trim().equals(loginReturnMess.trim()))) {
			return true;
		}
		return false;
	}

	public void init(WebDriverService webDriver, String username, String password,String uid,String plateformNane) {
		this.webDriver = webDriver;
		this.username = username;
		this.password = password;
		this.uid = uid;
		this.plateformNane = plateformNane;

	}

	@Override
	public void run() throws Exception {
		get();
		sendKeys();
		submit();
	}

	public boolean islogin() {
		return webDriver
				.isLogin("http://scs.suning.com/sps/portal/showPortalPage.action");
	}

	@Override
	public void fiter(List<String> list, Map<String, String> map) {
		// TODO Auto-generated method stub
		
	}

}
